package com.example.xmpic.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import androidx.core.content.ContextCompat;

import com.example.common.utils.DisplayUtils;
import com.example.xmpic.R;

/**
 * @author skygge
 * @date 2020-06-05.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：横向滑动控件
 */
public class TimerScrollView extends HorizontalScrollView {

    private static final int TOTAL_PROCESS_HOUR = 4320;
    private static final int TOTAL_PROCESS_MINUTE = 25920;

    private Context mContext;
    private LinearLayout mLayout;
    private HistoryView mHistoryView;
    private Handler mHandler;
    private ScrollViewListener mScrollViewListener;

    public enum ScrollType{IDLE,TOUCH_SCROLL,FLING}

    private final static int mMarginLeft = 14;

    /**
     * 记录当前滚动的距离
     */
    private int mCurrentX = -9999999;

    /**
     * 当前滚动状态
     */
    private ScrollType mScrollType = ScrollType.IDLE;

    private boolean isHourOrMinute = true;

    public TimerScrollView(Context context) {
        super(context);
        this.mContext = context;
        initView(context);
    }

    public TimerScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView(context);
    }

    public TimerScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context);
    }

    private void initView(Context context){
        mLayout = new LinearLayout(context);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mLayout.setOrientation(LinearLayout.HORIZONTAL);
        layoutParams.width = isHourOrMinute ? TOTAL_PROCESS_HOUR : TOTAL_PROCESS_MINUTE;
        mLayout.setLayoutParams(layoutParams);
        addView(mLayout);
        addHistoryView();
    }

    private void addHistoryView(){
        int screenWidth = DisplayUtils.getScreenWidth(mContext);
        int viewHeight = DisplayUtils.dip2px(mContext, 88f);
        int margin = (int)(mMarginLeft * getResources().getDisplayMetrics().scaledDensity);
        View emptyViewLeft = new View(mContext);
        LinearLayout.LayoutParams leftLayout= new LinearLayout.LayoutParams(screenWidth/2 - margin, LinearLayout.LayoutParams.MATCH_PARENT);
        emptyViewLeft.setBackgroundColor(ContextCompat.getColor(mContext, R.color.empty_history));
        emptyViewLeft.setLayoutParams(leftLayout);
        mLayout.addView(emptyViewLeft);

        LinearLayout.LayoutParams mHistoryLayout = new LinearLayout.LayoutParams(isHourOrMinute ? TOTAL_PROCESS_HOUR : TOTAL_PROCESS_MINUTE, viewHeight);
        mHistoryView = new HistoryView(mContext);
        mHistoryView.setLayoutParams(mHistoryLayout);
        mLayout.addView(mHistoryView);

        View emptyViewRight = new View(mContext);
        LinearLayout.LayoutParams rightLayout= new LinearLayout.LayoutParams(screenWidth/2, LinearLayout.LayoutParams.MATCH_PARENT);
        emptyViewRight.setBackgroundColor(ContextCompat.getColor(mContext, R.color.empty_history));
        emptyViewRight.setLayoutParams(rightLayout);
        mLayout.addView(emptyViewRight);
    }

    /**
     * 设置显示时间/分钟
     */
    public void setHourOrMinute(boolean hourOrMinute) {
        isHourOrMinute = hourOrMinute;
        mLayout.removeAllViews();
        addHistoryView();
    }

    public HistoryView getHistoryView() {
        return mHistoryView;
    }

    public interface ScrollViewListener {
        void onScrollChanged(ScrollType scrollType);
    }

    /**
     * 滚动监听runnable
     */
    private Runnable scrollRunnable = new Runnable() {

        @Override
        public void run() {
            if(mCurrentX == getScrollX()){
                //滚动停止  取消监听线程
                mScrollType = ScrollType.IDLE;
                if(mScrollViewListener!=null){
                    mScrollViewListener.onScrollChanged(mScrollType);
                }
                mHandler.removeCallbacks(this);
                return;
            }else{
                //手指离开屏幕    view还在滚动的时候
                mScrollType = ScrollType.FLING;
                if(mScrollViewListener!=null){
                    mScrollViewListener.onScrollChanged(mScrollType);
                }
            }
            mCurrentX = getScrollX();
            mHandler.postDelayed(this, 50);
        }
    };


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                this.mScrollType = ScrollType.TOUCH_SCROLL;
                mScrollViewListener.onScrollChanged(mScrollType);
                //手指在上面移动的时候   取消滚动监听线程
                mHandler.removeCallbacks(scrollRunnable);
                break;
            case MotionEvent.ACTION_UP:
                //手指移动的时候
                mHandler.post(scrollRunnable);
                break;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 必须先调用这个方法设置Handler  不然会出错
     */
    public void setHandler(Handler handler){
        this.mHandler = handler;
    }

    /**
     * 设置滚动监听
     */
    public void setOnScrollStateChangedListener(ScrollViewListener listener){
        this.mScrollViewListener = listener;
    }
}
